﻿//**********************************************************
//Copyright(C)2010 By 波特商网公司 版权所有。
//
//文件名：
//文件功能：左边操作功能菜单
//
//创建标识：米海波 || 2011-04-22
//
//修改标识：
//修改描述：
//**********************************************************
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Hmc.IM.Common.UserControls
{
    /// <summary>
    /// 左边功能操作菜单
    /// </summary>
    public partial class ucLiftMenu : UserControl
    {

        /// <summary>
        /// 选择按钮出发
        /// </summary>
        public event EventHandler<ClickedEventArgs> Selected;

        private Label _oldItem;
        private object dataSource;

        /// <summary>
        /// 绑定的数据源
        /// </summary>
        public object DataSource
        {
            get { return dataSource; }
            set { dataSource = value; }
        }
        private string displayMember;

        /// <summary>
        /// 显示的值
        /// </summary>
        public string DisplayMember
        {
            get { return displayMember; }
            set { displayMember = value; }
        }
        private string valueMember;

        /// <summary>
        /// 获取的值
        /// </summary>
        public string ValueMember
        {
            get { return valueMember; }
            set { valueMember = value; }
        }


        private string selectedText;
        //private string selectedValue;
        private object selectedItem;
        private int selectedIndex = -1;

        /// <summary>
        /// 标识是否允许更改选择项
        /// </summary>
        private bool isFay = false;

        /// <summary>
        /// 选择的对象索引
        /// </summary>
        public int SelectedIndex
        {
            get { return selectedIndex; }
            set
            {
                if (value > -1 && value < this.flpLayout.Controls.Count)
                {
                    selectedIndex = value;
                    this.isFay = true;
                    lb_Click(this.flpLayout.Controls[value], new EventArgs());
                }
                else if (value >= this.flpLayout.Controls.Count)
                {
                    throw new Exception("索引超出最大界限");
                }
            }
        }

        /// <summary>
        /// 标题文本
        /// </summary>
        [Browsable(true), Category("TitleText"), Description("关联的标题文本")]
        public string TitleText
        {
            get
            {
                return lbTitle.Text;
            }
            set
            {
                lbTitle.Text = value;
            }
        }


        private bool isMouseClick = true;
        /// <summary>
        /// 是否允许鼠标点击选择
        /// </summary>
        [Browsable(true),DefaultValue(true), Category("行为"), Description("是否允许鼠标点击选择")]
        public bool IsMouseClick
        {
            get { return isMouseClick; }
            set { isMouseClick = value; }
        }

        private Color borderColor = Color.FromArgb(232, 236, 239);

        /// <summary>
        /// 边框颜色
        /// </summary>
        [Browsable(true), Category("BorderColor"), Description("边框颜色")]
        public Color BorderColor
        {
            get { return borderColor; }
            set { borderColor = value; }
        }

        private Label lbTitle = new Label();

        /// <summary>
        /// item集合
        /// </summary>
        List<Label> list = new List<Label>();

        private int _childHeighNum = 1;

        /// <summary>
        /// 指示是否已添加垂直滚动条
        /// </summary>
        private bool _isVScroll = false;

        /// <summary>
        /// 自动显示垂直滚动条
        /// </summary>
        [Browsable(true), Category("AutoVScroll"), Description("自动显示垂直滚动条"),DefaultValue(true)]
        public bool AutoVScroll { get; set; }

        /// <summary>
        /// 垂直滚动条
        /// </summary>
        private VScrollBar scroll;

        /// <summary>
        /// 构造呈现内容
        /// </summary>
        public ucLiftMenu()
        {
            InitializeComponent();
            lbTitle.Font = new Font("宋体", 9, FontStyle.Bold);
            lbTitle.TextAlign = ContentAlignment.MiddleCenter;
            lbTitle.Parent = panel1;
            lbTitle.Dock = DockStyle.Fill;
            this.TitleText = "标题1";
        }

        /// <summary>
        /// 添加节点
        /// </summary>
        /// <param name="text">显示的值</param>
        /// <param name="tag">关联的数据对象</param>
        public void AddItem(string text, object tag)
        {
            Label lb = CreateItem(text, tag);
            lb.Parent = flpLayout;
            lb.Click += new EventHandler(lb_Click);
        }

        void lb_Click(object sender, EventArgs e)
        {
            if (IsMouseClick || this.isFay)
            {
                Label lb = sender as Label;
                this.selectedText = lb.Text;
                this.selectedItem = lb.Tag;
                //泛型对象需反射获取
                if (_oldItem != null)
                {
                    _oldItem.BackColor = Color.Transparent;
                }
                this._oldItem = lb;
                lb.BackColor = Color.FromArgb(213, 232, 239);
                this.selectedIndex = lb.TabIndex;
                if (Selected != null)
                {
                    Selected(this, new ClickedEventArgs(lb.Text, lb.Tag, lb.TabIndex));
                }
                this.isFay = false;
            }
        }

        /// <summary>
        /// 创建Item
        /// </summary>
        /// <param name="text">显示的值</param>
        /// <returns></returns>
        private Label CreateItem(string text)
        {
            Label lb = new Label();
            lb.AutoSize = false;
            lb.BackColor = Color.Transparent;
            lb.ForeColor = Color.FromArgb(48, 48, 50);
            lb.Cursor = Cursors.Hand;
            lb.Width = flpLayout.Width;
            lb.Margin = new System.Windows.Forms.Padding(0);
            lb.Height = 20;
            lb.Text = text;
            lb.TextAlign = ContentAlignment.MiddleCenter;
            list.Add(lb);
            //累加控件占用高度
            _childHeighNum += lb.Height;
            if (flpLayout.Height < _childHeighNum && !_isVScroll)
            {
                scroll = new VScrollBar();
                scroll.Height = flpLayout.Height;
                scroll.Parent = this;
                scroll.Location = new Point(flpLayout.Width - scroll.Width, flpLayout.Location.Y);
                scroll.Scroll += new ScrollEventHandler(scroll_Scroll);
                scroll.BringToFront();
                _isVScroll = true;
            }
            if (_isVScroll)
            {
                scroll.Maximum = list.Count;
            }
            return lb;
        }

        void scroll_Scroll(object sender, ScrollEventArgs e)
        {
            if (e.NewValue > e.OldValue)
            {
                list[e.OldValue].Visible = false;
            }
            else if (e.NewValue < e.OldValue || e.OldValue == 0)
            {
                list[e.OldValue].Visible = true;
            }
        }

        /// <summary>
        /// 创建Item
        /// </summary>
        /// <param name="text">显示的值</param>
        /// <param name="tag">附带的关联数据</param>
        /// <returns></returns>
        private Label CreateItem(string text ,object tag)
        {
            Label lb = this.CreateItem(text);
            lb.Tag = tag;
            return lb;
        }

        private void DataBanding(object source)
        {
            if (source.GetType().GetGenericTypeDefinition() == typeof(List<object>).GetGenericTypeDefinition())
            {
                List<object> list = new List<object>();
                foreach (object item in list)
                {
                    //反射读取属性信息
                }
            }
            else if(source is DataTable)
            {
                DataTable table = source as DataTable;
                BandingTable(table);
            }
            else if (source is DataView)
            {
                DataTable table = (source as DataView).Table;
                BandingTable(table);
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        /// <param name="table"></param>
        private void BandingTable(DataTable table)
        {
            foreach (DataRow row in table.Rows)
            {
                string displayString = string.Empty;
                try
                {
                    if (this.displayMember == string.Empty)
                    {
                        displayString = row.ToString();
                    }
                    else if (row[this.displayMember] != DBNull.Value)
                    {
                        displayString = row[this.displayMember].ToString();
                    }
                }
                catch (Exception ex)
                {
                    Logger.Write(ex);
                    MessageBox.Show(ex.Message, "消息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                }
                CreateItem(displayString, row);
            }
        }

        /// <summary>
        /// 控件大小发生改变时
        /// </summary>
        /// <param name="e"></param>
        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
        }
        /// <summary>
        /// 重绘
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Rectangle rect = this.DisplayRectangle;
            e.Graphics.DrawRectangle(new Pen(borderColor, 1), rect.X, rect.Y, rect.Width - 1, rect.Height - 1);
            e.Graphics.Save();
        }

    }
    /// <summary>
    /// 点击按钮事件
    /// </summary>
    public class ClickedEventArgs : EventArgs
    {
        private string _clickName;

        /// <summary>
        /// 点击的按钮名称
        /// </summary>
        public string ClickName
        {
            get { return _clickName; }
            set { _clickName = value; }
        }

        private object _tag;

        /// <summary>
        /// 用户数据
        /// </summary>
        public object Tag
        {
            get { return _tag; }
            set { _tag = value; }
        }

        private int _index;

        /// <summary>
        /// 当前索引
        /// </summary>
        public int Index
        {
            get { return _index; }
            set { _index = value; }
        }

        /// <summary>
        /// 初始化信息对象
        /// </summary>
        /// <param name="clickName">选择的按钮名称</param>
        /// <param name="tag"></param>
        /// <param name="index"></param>
        public ClickedEventArgs(string clickName,object tag,int index)
        {
            this._clickName = clickName;
            this.Tag = tag;
            this._index = index;
        }
    }
}
